// pages/03setup/02printer/printerSet.js
var app = getApp()
var appData = app.globalData
Page({
   data: {
      printerList: [],
      // typeArr: ['请选择', 'wifi', 'GPRS', '蓝牙', '网络', '其他'],
      typeList: '',
      isShowLabelArr: [0, 0, 0, 0, 0, 0, 0],
      mode: '',
      buttonText: '添加打印机',
      pick_i: '',
      edit_id: -1,
      OpenUserId: '',//获取对对机的OpenUserId
      getOUIdParam: {},//获取用的参数
   },

   onLoad: function (opt) {
      var t = this
      t.getSupportPrinter()
      var param = {
         shop_id: appData.shopInfo.Biz_ID,
         use: 'edit'
      }
      app.getNetData('print2/mgr/getprinterList', param, function (res) {
         t.setData({ printerList: res.data })
         console.log('t.data===', t.data)
      })
   },

   getOpenUserId: function () {
      var t = this
      var param = t.data.getOUIdParam
      param.userid = appData.shopInfo.Biz_ID
      app.getNetData('print2/mgr/getOpenUserId', param, function (res) {
         t.setData({ OpenUserId: res.data })
      })
   },
   //选择器
   pick: function (e) {
      var val = e.detail.value
      this.setData({ pick_i: val })
      this.resetShowLabelArr(e.currentTarget.id, val)
   },
   //输入框事件
   showLabel: function (e) {
      var td = this.data
      var val = e.detail.value
      var i = e.currentTarget.id
      if (td.pick_i == 1 && i >= 3) {//获取对对机OpenUserId准备参数
         console.log('td.pick_i===' + td.pick_i + '===i===' + i)
         var keys = td.typeList.params[td.pick_i]
         var key = keys[i - 3]
         td.getOUIdParam[key] = val
         console.log('getOUIdParam===', td.getOUIdParam)
      }
      // if (val.length > 1) return
      this.resetShowLabelArr(i, val)
   },
   //输入框重置标签
   resetShowLabelArr: function (i, val) {
      var td = this.data
      var arr = td.isShowLabelArr
      if (i == 0) {
         var keys = td.typeList.params[val]
         arr = [1]
         for (var j = 1; j < keys.length + 3; j++) {
            arr.push(0)
         }
         console.log('keys===', keys, 'arr===', arr)
      } else {
         arr[i] = val == '' ? 0 : 1
      }
      this.setData({ isShowLabelArr: arr })
   },

   getSupportPrinter: function () {
      var t = this
      if (t.data.typeList == '') {
         app.getNetData('print2/mgr/gettypelist', '', function (res) {
            t.setData({ typeList: res.data })
            console.log('【支持的打印机】', t.data)
         })
      }
   },

   addButtonChange: function () {
      this.setData({
         pick_i: '',
         mode: this.data.mode == '' ? 'add' : '',
         buttonText: this.data.mode == '' ? '取消添加' : '添加打印机',
         edit_id: -1,
         isShowLabelArr: [0, 0, 0, 0, 0, 0, 0],
      })
   },

   editPrinter: function (e) {
      this.getSupportPrinter()
      var td = this.data
      var index = e.currentTarget.id
      var printer = td.printerList[index]
      this.setData({
         edit_id: td.edit_id == index ? -1 : index,
         isShowLabelArr: index > -1 ? [1, 1, 1, 1, 1, 1, 1] : [0, 0, 0, 0, 0, 0, 0],
         pick_i: td.edit_id == index ? '' : printer.type,
         mode: '',
         buttonText: '添加打印机',
      })
      console.log('this.data==', td)
   },

   delConfirm: function (e) {
      var t = this
      app.confirm('重要提示', '确认删除吗？', '删除', function (res) {
         console.log('delConfirm: function', res)
         if (res) {
            t.delPrinter(e.currentTarget.id)
         }
      })
   },

   delPrinter: function (i) {
      var t = this
      var list = t.data.printerList
      var param = { printer_id: list[i].printer_id }
      app.getNetData('print2/mgr/delPrinter', param, function (res) {
         list.splice(i, 1)
         t.setData({ printerList: list })
      })
   },

   submitPrinter: function (e) {
      var t = this
      var td = this.data
      var param = e.detail.value
      if (td.edit_id > -1) {
         param.printer_id = td.printerList[td.edit_id].printer_id
      } else {
         param.type = td.pick_i
      }
      param.shop_id = appData.shopInfo.Biz_ID
      if (td.pick_i >= 0) {
         param.brand = td.typeList.name_cn[td.pick_i]
         param.brand_en = td.typeList.name_en[td.pick_i]
      } else {
         param.brand = ''
         param.brand_en = ''
      }
      console.log('e==', e, 'param==', param)
      app.getNetData('print2/mgr/setPrinter', param, function (res) {
         if (td.edit_id == -1) {
            t.addButtonChange()
         }
         t.setData({
            isShowLabelArr: [0, 0, 0, 0, 0, 0, 0],
            OpenUserId: '',
            edit_id: -1
         })
         t.onLoad()
      })
   },
})